package de.tum.in.eist.im.client.Chat.CreateChatChannelTab;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.im.client.Chat.ChatPanel;

public class CreateChatChannelTab  implements ClickHandler, KeyUpHandler {
	
	// private variables
	private ChatPanel chatPanel;
	private TextBox textBox = new TextBox();
	private static Button createGroupButton = new Button();
	
	/**
	 * constructor
	 * @param chatPanel reference to the chatPanel
	 */
	public CreateChatChannelTab(ChatPanel chatPanel) {
		this.chatPanel = chatPanel;
	}

	/**
	 * call the create group function if the user press enter
	 */
	@Override
	public void onKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
			this.createGroup();
	}

	/**
	 * call the create group function if the user clicks the button referencing this method
	 */
	@Override
	public void onClick(ClickEvent event) {
		this.createGroup();
	}
	
	/**
	 * create a new channel/tab/group
	 */
	public void createGroup() {
		createGroupButton.setEnabled(false);
		if (!this.textBox.getText().isEmpty())
			this.chatPanel.requestChatTab(this.textBox.getText());
	}
	
	/**
	 * builds the panel for the create tab/group tutorial
	 * @return the created widget. the widget is added as tab in the chatPanel
	 */
	public Widget getWidget() {
		// create a chat tutorial panel
		SimplePanel simplePanel = new SimplePanel();
		VerticalPanel verticalPanel = new VerticalPanel();

		// set the default text of the texBox
		textBox.setText("Please enter the group name!");
		verticalPanel.add(textBox);
		
		// set the name of the createGroupButton
		createGroupButton.setText("Create");
		verticalPanel.add(createGroupButton);
		
		// add the verticalPanel to the simple panel
		simplePanel.add(verticalPanel);
		
		// add handler
		textBox.addKeyUpHandler(this);
		createGroupButton.addClickHandler(this);
		
		// return the simple panel
		return simplePanel;
	}
	
	/**
	 * return the static create group button
	 * @return the static create group button
	 */
	public static Button getCreateGroupButton() {
		return createGroupButton;
	}
}
